package com.hxh.crud.controller;

import com.github.pagehelper.PageInfo;
import com.hxh.crud.bean.Employee;
import com.hxh.crud.service.EmployeeService;
import com.hxh.crud.util.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @CreatedBy: hxh
 * @Date: 2020/2/28 13:58
 * @Description: 员工控制器
 * @TODO: 加油吧！骚年！
 */
@CrossOrigin
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/emps")
    public CommonResponse getEmps(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestBody(required = false) Map<String, Object> example) {
        PageInfo<Employee> emps = employeeService.getEmps(pageNum, pageSize, example);
        return CommonResponse.success("查询成功！", emps);
    }

    @RequestMapping({"/checkEmp/{empName}", "/checkEmp/{empName}/{empId}"})
    public CommonResponse checkEmp(@PathVariable String empName,
                                   @PathVariable(required = false) String empId) {
        boolean flag = employeeService.checkEmp(empName, empId);
        if (flag) {
            return CommonResponse.failure("员工名重复，请更换员工名");
        }
        return CommonResponse.success("员工名未重复");
    }

    @PostMapping("/emp")
    public CommonResponse addEmp(@Valid @RequestBody Employee employee, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            Map<String, String> map = new HashMap<>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return CommonResponse.failure("添加失败，请检查所填内容！", map);
        }
        employeeService.addEmp(employee);
        return CommonResponse.success("添加成功！");
    }

    @GetMapping("/emp/{empId}")
    public CommonResponse getEmpById(@PathVariable String empId) {
        Employee employee = employeeService.getEmpById(empId);
        return CommonResponse.success("查询成功！", employee);
    }

    @PutMapping("/emp")
    public CommonResponse editEmp(@Valid @RequestBody Employee employee, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            Map<String, String> map = new HashMap<>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return CommonResponse.failure("修改失败，请检查所填内容！", map);
        }
        employeeService.editEmp(employee);
        return CommonResponse.success("修改成功！");
    }

    @DeleteMapping("/emp")
    public CommonResponse delEmp(@RequestBody List<Employee> employeeList) {
        employeeService.delEmp(employeeList);
        return CommonResponse.success("删除成功！");
    }

}
